package com.example.demo.java8;



import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public abstract class DateUtils {

    private static final String YMD_FORMAT = "yyyy-MM-dd";
    private static final String YMDHMS_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static final String YMD_FORMAT_NEW = "yyyyMMdd";

    public static Calendar toCalendar(Date date) {
        if (date == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static String toYMD(Date date) {
        if (date == null){
            return null;
        }
        return DateFormatUtils.format(date, YMD_FORMAT);
    }

    public static String toYMD_NEW(Date date) {
        if (date == null){
            return null;
        }
        return DateFormatUtils.format(date, YMD_FORMAT_NEW);
    }

    public static String toYMDHMS(Date date) {
        if (date == null){
            return null;
        }
        return DateFormatUtils.format(date, YMDHMS_FORMAT);
    }

    public static Date toYMD(String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YMD_FORMAT);
        Date date = null;
        try {
            date = simpleDateFormat.parse(str);
        } catch (Exception e) {
        }
        return date;
    }

    public static Date toYMDHMS(String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YMDHMS_FORMAT);
        Date date = null;
        try {
            date = simpleDateFormat.parse(str);
        } catch (Exception e) {
        }
        return date;
    }

    public static String toYMD(Date date, String format) {
        if (date == null){
            return null;
        }
        return DateFormatUtils.format(date, format);
    }
}
